$(function() {
	$("#submitButton").click(checkAndSubmit);
	$("#article_title").bind({
		"click": function() {
			// 判断是否登录, 放在后台过滤器中进行
		},
		"blur": function() {
			$(this).css({"border": "1px dashed #CCCCCC"});
		},
		"mouseover": function() {
			$(this).css({"border": "2px solid #FFC600"});
		},
		"mouseout": function() {
			$(this).css({"border": "1px dashed #CCCCCC"});
		}
	});
});

// 初始化插件
var articleEditor_inputEditor;

KindEditor.ready(function(K) {
	var editor = K.create('textarea[id="inputTextArea"]', {
		//cssPath : 'plugins/editor/plugins/code/prettify.css',
		uploadJson : '/uploads/upload_json.jsp',
		//fileManagerJson : 'plugins/editor/jsp/file_manager_json.jsp',
		allowFileManager : true,
		resizeType : 0,
		minWidth: 400,
		minHeight: 180,
		items : [
			'emoticons', 'image', 'fontname', 'fontsize', 
			'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
			'justifyleft', 'justifycenter', 'justifyright'],
		afterUpload : function(url) {
			callback(url);
        }
	});
	prettyPrint();
	articleEditor_inputEditor = editor;
});
function checkAndSubmit() {
	articleEditor_inputEditor.sync();// 同步textarea和KindEditor之间的数据
	var articleTitle = $("#article_title").val();
	if (!CHECK.isEmpty(articleTitle)) {
		if (!articleEditor_inputEditor.isEmpty("inputTextArea")) {
			$("#articleForm").submit();
		} else {
			alert("请输入帖子内容");
		}
	} else {
		alert("请给帖子一个标题");
	}
}
function callback(fullImgSrc)
{
	$("#outer").css("display","block");
	$("#yuantu").html("<img id=cropbox style='max-width:360px ;max-height:370px'>");
	$("#preview_box").html("<img id=crop_preview>");
	$("#cropbox").attr("src",fullImgSrc);
	$("#crop_preview").attr("src",fullImgSrc);
	$("#imgPath").val(fullImgSrc);
	$(function(){
		$('#cropbox').Jcrop({
			onChange:showPreview,
			onSelect:showPreview,
			aspectRatio:1,
			setSelect:[0,0,100,100]
		});
	
		function showPreview(coords){
			if(parseInt(coords.w) > 0){
				//计算预览区域图片缩放的比例，通过计算显示区域的宽度(与高度)与剪裁的宽度(与高度)之比得到
				var imgw=$("#crop_preview").width();
				var rx = $("#preview_box").width() / coords.w; 
				var ry = $("#preview_box").height() / coords.h;
				
				//通过比例值控制图片的样式与显示
				$("#crop_preview").css({
					width:Math.round(rx * $("#cropbox").width()) + "px",	//预览图片宽度为计算比例值与原图片宽度的乘积
					height:Math.round(rx * $("#cropbox").height()) + "px",	//预览图片高度为计算比例值与原图片高度的乘积
					marginLeft:"-" + Math.round(rx * coords.x) + "px",
					marginTop:"-" + Math.round(ry * coords.y) + "px"
				});
	
				$('#x').val(coords.x);
				$('#y').val(coords.y);
				$('#w').val(coords.w );
				$('#h').val(coords.h );
			}
		}
	});
}

function UploadZuoBiao(){
	
	var submitPath = $("#submitPath").val();
	var yuanW=$("#cropbox").width();//图片的宽
	var yuanH=$("#cropbox").height();//图片的高
	var x = $("#x").val();
	var y = $("#y").val();
	var w = $("#w").val();
	var h = $("#h").val();
	var imgPath = $("#imgPath").val();
	$.ajax({
		url: submitPath,
		data:{
			'x':x,
			'y':y,
			'w':w,
			'h':h,
			'yuanW':yuanW,
			'yuanH':yuanH,
			'imgPath':imgPath
			},
		type: 'post',
		async: false,
		dataType: 'json',
		success: function(res) {
				if(res.result == 0){
					//alert(res.url);
					//$('#headUrl').html("<img src="+res.url+" style='width:80px ;height:80px'>");
					//$("#imgPath").val(res.url);
					$('#outer').css("display","none");
					
				}
				else{
					alert("系统处理异常！错误信息：" + res.message);
				}
			}
});
}